package com.xuecheng.order.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.api.order.model.vo.PayResultModel;
import com.xuecheng.common.enums.order.OrderDealStatusEnum;
import com.xuecheng.common.util.JsonUtil;
import com.xuecheng.order.entity.Orders;
import com.xuecheng.order.entity.Pay;
import com.xuecheng.order.service.OrdersService;
import com.xuecheng.order.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
@RocketMQTransactionListener
@Slf4j
public class ProduceTxmsgPayListener implements RocketMQLocalTransactionListener {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private PayService payService;

    /**
     * 业务分析
     * 1.解析消息并封装
     * 2.调用service层执行本地事务
     * 3.返回结果给mq
     * commit
     * rollback
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        try {
            //1.解析消息并封装
            String jsonString = new String((byte[]) message.getPayload());
            PayResultModel resultModel = JsonUtil.jsonToObject(jsonString, PayResultModel.class);
            //2.调用service层执行本地事务
            this.ordersService.successPayment(resultModel);
            //3.返回结果给mq

            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            log.error("执行本地事务失败:{}", e.getMessage());
            System.out.println("执行本地事务失败:" + e.getMessage());
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {

        //1.解析消息并封装
        String jsonString = new String((byte[]) message.getPayload());
        PayResultModel resultModel = JsonUtil.jsonToObject(jsonString, PayResultModel.class);

        //判断本地执行事务的结果
        //* 1.判断订单数据
        Orders orders = this.ordersService.getById(resultModel.getOrder().getOrderId());
        if (ObjectUtils.isEmpty(orders)) {
            //获取支付失败的消息
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        //* 2.根据order中的订单号获取订单支付数据(OrderPay)
        LambdaQueryWrapper<Pay> queryPayWrapper = new LambdaQueryWrapper<>();
        queryPayWrapper.eq(Pay::getOrderId, orders.getId());
        Pay pay = this.payService.getOne(queryPayWrapper);
        if (ObjectUtils.isEmpty(pay)) {
            //获取支付失败的消息
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        //判断订单是否支付Order===>Status
        if (!(OrderDealStatusEnum.ORDER_DEAL_PAID_STATUS.getCode().equals(orders.getStatus().toString()))) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        // 判断课程支付是否支付 OrderPay===>Status
        if (!(Pay.PAY_FAILED_STATUS_FLAG.equals(pay.getStatus()))) {
            return RocketMQLocalTransactionState.ROLLBACK;
        }
        return RocketMQLocalTransactionState.COMMIT;
    }
}
